package com.juzhencms.apps.busi.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.juzhencms.apps.busi.service.AdService;
import com.juzhencms.apps.busi.service.AdminMenuService;

import lombok.extern.slf4j.Slf4j;

import com.juzhencms.apps.base.busi.service.UploadService;
import com.juzhencms.apps.base.busi.service.UploadService.UploadFileParam;
import com.juzhencms.apps.base.busi.service.UploadService.UploadResult;
import com.juzhencms.apps.base.common.config.Config;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.FileUtil;
import com.juzhencms.apps.base.common.util.StringUtil;
import com.juzhencms.apps.base.common.util.WebUtil;


@Controller
@RequestMapping("/weiXinEidt")
@Slf4j
public class WeiXinEidtController extends BaseController{
	@Autowired
	private UploadService uploadService;
	
	@RequestMapping(value="/eidtController",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String eidtController(@RequestParam(value = "upfile", required = false) MultipartFile file,HttpServletRequest request,HttpServletResponse response){
		
		//log.info("getLoginUserId:"+this.getLoginUserId());
		
		if(StringUtil.isNotNullOrEmpty(request.getParameter("action"))) {
			String action=request.getParameter("action")+"";
			if("config".equals(action)) {
				InputStream stream = getClass().getClassLoader().getResourceAsStream("weiXinEditConfig.json");
				try {
					BufferedReader r = new BufferedReader(new InputStreamReader(stream, "UTF-8")); 
					StringBuffer buffer = new StringBuffer();
					String line = " ";
				
					while ((line = r.readLine()) != null){
					     buffer.append(line);
					}
					LinkedHashMap weiXinEditConfig = JSON.parseObject(buffer.toString(),LinkedHashMap.class,Feature.OrderedField);
					
					weiXinEditConfig.put("uploadUrlPath", Config.getUploadUrlPath());
					return WebUtil.resultString(weiXinEditConfig,request);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					log.error("读取weiXinEditConfig.json出错",e);
				}
			}else if("uploadimage".equals(action)){//上传图片
				//log.info("fileName===="+file.getOriginalFilename());
				String original=file.getOriginalFilename();
					//提示
					Map<String,Object> msgMap = new HashMap<>();
					
					String type="img";
					
					Map allowType=new HashMap();
					allowType.put("img", new String[]{"jpeg","jpg","bmp","gif","png"});
					
					Map allowTypeSize=new HashMap();
					allowTypeSize.put("file", 20);
					allowTypeSize.put("img", 5);
					allowTypeSize.put("flash", 5);
					
					String extName=FileUtil.getExtName(file.getOriginalFilename());
					
					String[] typeArr=(String[])allowType.get(type);
					if(typeArr==null){
						Map data=new HashMap();
						data.put("uploaded", 0);
						msgMap.put("message","错误的类型参数");
						data.put("error",msgMap);
						return WebUtil.resultString(data,request);			
					}
					
					
					int size=(int) file.getSize();
					
					UploadFileParam uploadFileParam=uploadService.new UploadFileParam();
					uploadFileParam.setAllowType(typeArr);
					uploadFileParam.setAllowFileSize((int)allowTypeSize.get(type));
					UploadResult uploadResult=uploadService.uploadFile(file, uploadFileParam);
					
					
				/*	{state: "SUCCESS", url: "/Uploads/ueditor/image/20201120/1605850028682505.jpg",…}
					original: "img7.jpg"
					size: 34114
					state: "SUCCESS"
					title: "1605850028682505.jpg"
					type: ".jpg"
					url: "/Uploads/ueditor/image/20201120/1605850028682505.jpg"
					*/
					Map data=new HashMap();
					if(uploadResult.getResult()==1) {
						data.put("state", "SUCCESS");
						data.put("title", (new File(uploadResult.getSavePath())).getName());
						data.put("size",size);
						data.put("type",extName);
						data.put("uploaded", 1);						
						data.put("original", original);
						data.put("fileName", (new File(uploadResult.getSavePath())).getName());
						data.put("url",uploadResult.getUrlPath());
					}else {
						data.put("uploaded", 0);
						msgMap.put("message",uploadResult.getMsg());
						data.put("error",msgMap);
					}
					return WebUtil.resultString(data,request);
				
				
			}else if("uploadvideo".equals(action)){//上传视频
				//log.info("fileName===="+file.getOriginalFilename());
				String original=file.getOriginalFilename();
					//提示
					Map<String,Object> msgMap = new HashMap<>();
					
					String type="video";
					
					Map allowType=new HashMap();
					allowType.put("video", new String[]{"mp4"});
					
					Map allowTypeSize=new HashMap();
					allowTypeSize.put("file", 20);
					allowTypeSize.put("img", 5);
					allowTypeSize.put("video", 10);
					allowTypeSize.put("flash", 5);
					
					String extName=FileUtil.getExtName(file.getOriginalFilename());
					
					String[] typeArr=(String[])allowType.get(type);
					if(typeArr==null){
						Map data=new HashMap();
						data.put("uploaded", 0);
						msgMap.put("message","错误的类型参数");
						data.put("error",msgMap);
						return WebUtil.resultString(data,request);			
					}
					
					
					int size=(int) file.getSize();
					
					UploadFileParam uploadFileParam=uploadService.new UploadFileParam();
					uploadFileParam.setAllowType(typeArr);
					uploadFileParam.setAllowFileSize((int)allowTypeSize.get(type));
					UploadResult uploadResult=uploadService.uploadFile(file, uploadFileParam);
					
					
				/*	{state: "SUCCESS", url: "/Uploads/ueditor/image/20201120/1605850028682505.jpg",…}
					original: "img7.jpg"
					size: 34114
					state: "SUCCESS"
					title: "1605850028682505.jpg"
					type: ".jpg"
					url: "/Uploads/ueditor/image/20201120/1605850028682505.jpg"
					*/
					Map data=new HashMap();
					if(uploadResult.getResult()==1) {
						data.put("state", "SUCCESS");
						data.put("title", (new File(uploadResult.getSavePath())).getName());
						data.put("size",size);
						data.put("type",extName);
						data.put("uploaded", 1);						
						data.put("original", original);
						data.put("fileName", (new File(uploadResult.getSavePath())).getName());
						data.put("url",uploadResult.getUrlPath());
					}else {
						data.put("uploaded", 0);
						msgMap.put("message",uploadResult.getMsg());
						data.put("error",msgMap);
					}
					return WebUtil.resultString(data,request);
				
				
			}else if("uploadmusic".equals(action)){//上传视频
				//log.info("fileName===="+file.getOriginalFilename());
				String original=file.getOriginalFilename();
					//提示
					Map<String,Object> msgMap = new HashMap<>();
					
					String type="music";
					
					Map allowType=new HashMap();
					allowType.put("music", new String[]{"mp3"});
					
					Map allowTypeSize=new HashMap();
					allowTypeSize.put("file", 20);
					allowTypeSize.put("img", 5);
					allowTypeSize.put("flash", 5);
					allowTypeSize.put("music", 2);
					String extName=FileUtil.getExtName(file.getOriginalFilename());
					
					String[] typeArr=(String[])allowType.get(type);
					if(typeArr==null){
						Map data=new HashMap();
						data.put("uploaded", 0);
						msgMap.put("message","错误的类型参数");
						data.put("error",msgMap);
						return WebUtil.resultString(data,request);			
					}
					
					
					int size=(int) file.getSize();
					
					UploadFileParam uploadFileParam=uploadService.new UploadFileParam();
					uploadFileParam.setAllowType(typeArr);
					uploadFileParam.setAllowFileSize((int)allowTypeSize.get(type));
					UploadResult uploadResult=uploadService.uploadFile(file, uploadFileParam);
					
					
				/*	{state: "SUCCESS", url: "/Uploads/ueditor/image/20201120/1605850028682505.jpg",…}
					original: "img7.jpg"
					size: 34114
					state: "SUCCESS"
					title: "1605850028682505.jpg"
					type: ".jpg"
					url: "/Uploads/ueditor/image/20201120/1605850028682505.jpg"
					*/
					Map data=new HashMap();
					if(uploadResult.getResult()==1) {
						data.put("state", "SUCCESS");
						data.put("title", (new File(uploadResult.getSavePath())).getName());
						data.put("size",size);
						data.put("type",extName);
						data.put("uploaded", 1);						
						data.put("original", original);
						data.put("fileName", (new File(uploadResult.getSavePath())).getName());
						data.put("url",uploadResult.getUrlPath());
					}else {
						data.put("uploaded", 0);
						msgMap.put("message",uploadResult.getMsg());
						data.put("error",msgMap);
					}
					return WebUtil.resultString(data,request);
				
				
			}
		}
		
		
		return null;
	}
}
